package com.zzh.takeoutapp.activity;

import android.content.Intent;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.base.BaseActivity;
import com.zzh.takeoutapp.event.NetMsgEvent;
import com.zzh.takeoutapp.model.AddressBean;
import com.zzh.takeoutapp.service.EventbusHelper;
import com.zzh.takeoutapp.service.ServiceUrl;
import com.zzh.takeoutapp.utils.LoadingUtil;
import com.zzh.takeoutapp.utils.PhoneUtils;
import com.zzh.takeoutapp.utils.ToolBarHelper;
import com.zzh.takeoutapp.utils.UtilTool;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/31.
 */

public class AddAddressActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    @Bind(R.id.toolbar)
    Toolbar tbToolbar;
    @Bind(R.id.ev_user_name)
    AppCompatEditText ev_user_name;
    @Bind(R.id.ev_mobile)
    AppCompatEditText ev_mobile;
    @Bind(R.id.ev_house_number)
    AppCompatEditText ev_house_number;
    @Bind(R.id.select_address)
    LinearLayout select_address;
    @Bind(R.id.radioGroup)
    RadioGroup radioGroup;
    @Bind(R.id.tv_select)
    TextView tv_select;
    @Bind(R.id.address_submit)
    TextView address_submit;
    private int ADD_ADDRESS = 1;
    private final String ADD_ADDRESS_ACTIVITY = "AddAddressActivity";
    private String longitude = "";
    private String latitude = "";
    private String address = "";
    private LoadingUtil loadingUtil;
    private AddressBean addressBean;
    private String api = "";
    private final String ADDRESS = "AddressActivity";

    @Override
    public void setContent() {
        setContentView(R.layout.activity_add_address);
        ButterKnife.bind(this);
    }

    @Override
    public void initComponent() {
        ToolBarHelper.setTitleAndBackAndText(this, tbToolbar, "新增收货地址");
        radioGroup.check(R.id.boy);
        loadingUtil = new LoadingUtil(this);
        addressBean = (AddressBean) getIntent().getSerializableExtra("addressBean");
        if (addressBean != null) {
            api = "client/member/addr/update";
        } else {
            api = "client/member/addr/create";
        }
        if (addressBean != null) {
            address = addressBean.getAddr();
            latitude = addressBean.getLat();
            longitude = addressBean.getLng();
            tv_select.setText(addressBean.getAddr());
            ev_user_name.setText(addressBean.getContact());
            ev_mobile.setText(String.valueOf(addressBean.getMobile()));
            ev_house_number.setText(addressBean.getHouse());
            if (addressBean.getType().equals("0")) {
                radioGroup.check(R.id.boy);
            } else {
                radioGroup.check(R.id.girl);
            }
        }
    }

    private boolean isStr() {
        if (addressBean != null) {
            if (moveToStr(ev_user_name).equals(addressBean.getContact())
                    && moveToStr(ev_mobile).equals(addressBean.getMobile())
                    && moveToStr(ev_house_number).equals(addressBean.getHouse())
                    && tv_select.getText().toString().equals(addressBean.getAddr())) {
                UtilTool.toast(this, "地址不变");
                return false;
            }
        }
        return true;
    }

    @Override
    public void setListener() {
        radioGroup.setOnCheckedChangeListener(this);
    }

    private String moveToStr(AppCompatEditText editText) {
        return editText.getText().toString();
    }

    private boolean isNull() {
        if (moveToStr(ev_user_name).equals("")) {
            UtilTool.toast(this, "请填写收货姓名");
        } else if (PhoneUtils.phoneVerify(this, moveToStr(ev_mobile)) == false) {
        } else if (moveToStr(ev_house_number).equals("")) {
            UtilTool.toast(this, "请填写门牌号");
        } else if (address.equals("")) {
            UtilTool.toast(this, "请选择地址");
        } else {
            return true;
        }
        return false;
    }


    @Override
    public void onAfterInit() {

    }

    @OnClick({R.id.select_address, R.id.address_submit})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.select_address:
                start_ActivityForResult(new Intent(this, AddLocationActivity.class), ADD_ADDRESS);
                break;
            case R.id.address_submit:
                if (isNull() == true) {
                    loadingUtil.startShowLoading();
                    if (addressBean != null && isStr() == true) {
                        ServiceUrl.AddrCreateServiceUrl(this,
                                api,
                                addressBean.getAddr_id(),
                                longitude,
                                address,
                                moveToStr(ev_house_number),
                                "1",
                                moveToStr(ev_user_name),
                                latitude,
                                moveToStr(ev_mobile),
                                ADD_ADDRESS_ACTIVITY);
                    } else {
                        ServiceUrl.AddrCreateServiceUrl(this,
                                "client/member/addr/create",
                                longitude,
                                address,
                                moveToStr(ev_house_number),
                                "1",
                                moveToStr(ev_user_name),
                                latitude,
                                moveToStr(ev_mobile),
                                ADD_ADDRESS_ACTIVITY);
                    }
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == ADD_ADDRESS && data != null) {
            tv_select.setText(data.getStringExtra("name"));
            longitude = data.getStringExtra("longitude");
            latitude = data.getStringExtra("latitude");
            address = data.getStringExtra("address");
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        EventbusHelper.register(this);
    }

    @Override
    public void onStop() {
        EventbusHelper.unregister(this);
        loadingUtil.stopShowLoading();
        super.onStop();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsgEvent(NetMsgEvent event) {
        String _msgType = (String) event.getMsgType();
        JSONObject jsonObject = event.getMsgJSONObj();
        switch (_msgType) {
            case ADD_ADDRESS_ACTIVITY:
                loadingUtil.stopShowLoading();
                if (jsonObject != null)
                    if (jsonObject.optString("message").equals("success")) {
                        Log.e("jsonObject", jsonObject.toString());
                        ServiceUrl.AddressServiceUrl(this, ADDRESS);
                        finish_Activity();
                    }
                break;
        }

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.boy:
                break;
            case R.id.girl:
                break;
        }
    }
}
